package com.example.app.controller;

import com.example.app.config.AuthConfig;
import com.example.app.entity.ReqData;
import com.example.app.service.AuthService;
import com.example.app.util.HttpClient;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {
    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private AuthService authService;
    @Autowired
    private AuthConfig authConfig;
    @PostMapping("sign")
    public Object getSign(@RequestParam String appID,@RequestParam  String appSecret,String authHost,String redirectUrl,String responseType) throws Exception {
        authConfig.setAppId(appID);
        authConfig.setAppSecret(appSecret);
        authConfig.setAuthHost(authHost);
        authConfig.setRedirectUrl(redirectUrl);
        authConfig.setResponseType(responseType);
        return authService.getObject(appID, appSecret,null);
    }

    /**
     * 获取token
     * @param url
     * @param sign
     * @return
     * @throws Exception
     */
    @PostMapping("token")
    public Object getToken(@RequestParam String url,@RequestParam  String sign){
        return HttpClient.postClient(url,sign);
    }
    /**
     * 获取用户信息
     * @return
     * @throws Exception
     */
    @PostMapping("userinfo")
    public Object getToken(HttpSession session){
        Map<String,String>  userinfo = (Map<String,String> ) session.getAttribute(AuthConfig.USER_INFO);
        if (userinfo==null){
            throw new NullPointerException("请登录");
        }
        return JSONObject.fromObject(userinfo).toString();
    }
    /**
     * 获取accessToken
     */
    @PostMapping("getaccessToken")
    public Object getAccessToken(HttpSession session){
        String openid = (String) session.getAttribute(AuthConfig.OPEN_ID);
        if (openid==null){
            throw new NullPointerException("请先获取用户信息");
        }
        Map<String, String> map = new HashMap<>();
        map.put("openid",openid);
        ReqData sign = authService.getSign(map);
        String postClient = HttpClient.postClient(authConfig.getAuthRest() + "accessToken", JSONObject.fromObject(sign).toString());

        LOG.info(postClient);
        JSONObject jsonObject = JSONObject.fromObject(postClient);

        JSONObject rsp = jsonObject.getJSONObject("rsp");
        String accessToken = rsp.getString("accessToken");
        session.setAttribute(AuthConfig.ACCESS_TOKEN,accessToken);
        return rsp;
    }

    public static void main(String[] args) {

        String patt = ".*\\s+(([7-9]\\d{1})|([7-9]\\d{1}\\.\\d+))\\%\\s+\\/.*";
        String line = "/dev/vda1        50G   13G   35G  70% /";
        System.out.println(line.matches(patt));;
    }
}
